﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration;
using SIPO.Library.DataAccess.Administration;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;

namespace SIPO.Web.Forms.Transaction
{
    public partial class recCorporateActionform : BasePage
    {
        private int iD = 0;

        protected override void OnLoad(EventArgs e)
        {

            if (Request.Params["id"] != null)
            {
                iD = Convert.ToInt32(Request.Params["id"]);
            }

            fsKonfirmasi.Attributes.CssStyle.Add("display", "none");
            btnSubmit.Attributes.CssStyle.Add("display", "none");
            btnBack.Attributes.CssStyle.Add("display", "none");

            if (!IsPostBack)
            {
                recDebtorRepo recDebtorDal = new recDebtorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recCorporateActionTypeRepo recCorporateActionTypeDal = new recCorporateActionTypeRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recRegionRepo recRegionDal = new recRegionRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

                List<recRegion> regions = recRegionDal.GetList();
                ddlWilayah.Items.Clear();
                regions.ForEach(delegate(recRegion region)
                {
                    ddlWilayah.Items.Add(new ListItem(region.RegionCode + " - " + region.RegionName, region.RegionID.ToString()));
                });
                ddlWilayah.Items.Insert(0, new ListItem("-- pilih wilayah  --", string.Empty));

                ddlKota.Items.Clear();
                ddlKota.Items.Insert(0, new ListItem("-- pilih kota --", string.Empty));

                List<recCorporateActionType> actions = recCorporateActionTypeDal.GetList();
                ddlCorporateActionType.Items.Clear();
                if (this.UserLoggedIn.ToLower().Equals("anonymous"))
                {
                    var publicActions = actions.FindAll(x => x.IsPublicUser.Value == true);
                    publicActions.ForEach(delegate(recCorporateActionType corporateAction)
                    {
                        ddlCorporateActionType.Items.Add(new ListItem(corporateAction.CorporateActionName, corporateAction.CorporateActionTypeID.ToString()));
                    });

                }
                else
                {
                    actions.ForEach(delegate(recCorporateActionType corporateAction)
                    {
                        ddlCorporateActionType.Items.Add(new ListItem(corporateAction.CorporateActionName, corporateAction.CorporateActionTypeID.ToString()));
                    });
                }
                ddlCorporateActionType.Items.Insert(0, new ListItem("-- pilih jenis aksi korporasi --", string.Empty));

                if (iD > 0)
                {
                    recCorporateActionRepo recCorporateActionDal = new recCorporateActionRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recCorporateAction recCorporateAction = recCorporateActionDal.GetById(iD);

                    recCityRepo recCityDal = new recCityRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

                    List<recCity> cities = recCityDal.GetList();
                    ddlKota.DataSource = cities;
                    ddlKota.DataTextField = recCity.Fields.CityName;
                    ddlKota.DataValueField = recCity.Fields.CityID;
                    ddlKota.DataBind();
                    ddlKota.Items.Insert(0, new ListItem("-- pilih kota  --", string.Empty));

                    if (recCorporateAction != null)
                    {
                        hfID.Value = iD.ToString();
                        txtCorporateName.Text = recCorporateAction.CorporateName;
                        txtCorporateSector.Text = recCorporateAction.CorporateSector;
                        txtNPWP.Text = recCorporateAction.NPWP;
                        txtAlamat1.Text = recCorporateAction.Address1;
                        txtAlamat2.Text = recCorporateAction.Address2;
                        txtAlamat3.Text = recCorporateAction.Address3;
                        txtTelpon.Text = recCorporateAction.Phone;
                        txtKodePos.Text = recCorporateAction.ZipCode;
                        txtEmail.Text = recCorporateAction.Email;
                        txtWebsite.Text = recCorporateAction.Website;
                        txtFax.Text = recCorporateAction.Fax;

                        txtNilaiEmisi.Text = recCorporateAction.ImpositionValue.HasValue ? recCorporateAction.ImpositionValue.Value.ToString() : "0";

                        if (recCorporateAction.RegionID != null)
                            ddlWilayah.Items.FindByValue(recCorporateAction.RegionID.ToString()).Selected = true;

                        if (recCorporateAction.CorporateActionTypeID != null)
                            ddlCorporateActionType.Items.FindByValue(recCorporateAction.CorporateActionTypeID.ToString()).Selected = true;

                        if (recCorporateAction.CityID != null)
                            ddlKota.Items.FindByValue(recCorporateAction.CityID.ToString()).Selected = true;

                        if (recCorporateAction.DebtorID != null)
                            ddlDebtor.Items.FindByValue(recCorporateAction.DebtorID.ToString()).Selected = true;

                        txtNilaiEmisi.Enabled = false;
                        ddlCorporateActionType.Enabled = false;
                        ddlDebtor.Enabled = false;

                        if (ddlDebtor.SelectedIndex > 0)
                        {
                            recDebtorSectorRepo recDebtorSectorDal = new recDebtorSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                            List<recDebtorSector> debtorSectors = recDebtorSectorDal.GetListByDebtorCorporateAction(int.Parse(ddlDebtor.SelectedValue), "2", "4,5");

                            if (!string.IsNullOrEmpty(base.SubSubSectorIDs))
                            {
                                string[] datas = base.SubSubSectorIDs.Split(',');
                                List<recDebtorSector> filter = debtorSectors.Where(m => datas.Contains(m.SubSubSectorID.ToString())).ToList();
                                filter.ForEach(delegate(recDebtorSector data)
                                {
                                    // Format Value -> SectorCode | SubSectorCode | SubSubSectorID | DebtorSectorID
                                    ddlSubSubSector.Items.Add(new ListItem(data.recSector.SectorName + " - " + data.recSubSector.SubSectorName + " - " + data.recSubSubSector.SubSubSectorName, data.recSector.SectorCode + "|" + data.recSubSector.SubSectorCode + "|" + data.SubSubSectorID + "|" + data.DebtorSectorID));                                    
                                });
                            }
                        }

                        hfImpositionBaseID.Value = recCorporateAction.ImpositionBaseID.HasValue ? recCorporateAction.ImpositionBaseID.Value.ToString() : string.Empty;
                        hfDebtorSectorID.Value = recCorporateAction.DebtorSectorID.HasValue ? recCorporateAction.DebtorSectorID.Value.ToString() : string.Empty;

                        hfBillingNo.Value = recCorporateAction.BillingNo;
                        hfInvoiceNo.Value = recCorporateAction.InvoiceNo;
                        hfInvoiceDate.Value = (recCorporateAction.InvoiceDate.HasValue ? recCorporateAction.InvoiceDate.Value.ToString("MM-dd-yyyy") : string.Empty);
                        hfCorporateActionDate.Value = (recCorporateAction.CorporateActionDate.HasValue ? recCorporateAction.CorporateActionDate.Value.ToString("MM-dd-yyyy") : string.Empty);
                    }
                }
                else
                {
                    if (base.DebtorID.HasValue)
                    {
                        recDebtor debtor = recDebtorDal.GetById(base.DebtorID.Value);
                        txtCorporateName.Text = debtor.DebtorName;
                        txtAlamat1.Text = debtor.Address1;
                        txtAlamat2.Text = debtor.Address2;
                        txtAlamat3.Text = debtor.Address3;
                        txtEmail.Text = debtor.Email;
                        txtFax.Text = debtor.Fax;
                        txtKodePos.Text = debtor.ZipCode;
                        txtNPWP.Text = debtor.NPWP;
                        txtTelpon.Text = debtor.Phone;
                        txtWebsite.Text = debtor.Website;


                        trCorporateName.Attributes.CssStyle.Add("display", "none");
                        trCorporateSector.Attributes.CssStyle.Add("display", "none");
                        trNPWP.Attributes.CssStyle.Add("display", "none");
                        trAddress1.Attributes.CssStyle.Add("display", "none");
                        trAddress2.Attributes.CssStyle.Add("display", "none");
                        trAddress3.Attributes.CssStyle.Add("display", "none");
                        trRegion.Attributes.CssStyle.Add("display", "none");
                        trCity.Attributes.CssStyle.Add("display", "none");
                        trEmail.Attributes.CssStyle.Add("display", "none");
                        trZipCode.Attributes.CssStyle.Add("display", "none");
                        trWebSite.Attributes.CssStyle.Add("display", "none");
                        trPhone.Attributes.CssStyle.Add("display", "none");

                    }

                }


            }


        }

    }
}